#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Ford UK NGc2 爬虫基础设施模块
包含WebDriver管理、等待机制、基础工具方法等
"""

import os
import time
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import (
    TimeoutException, 
    NoSuchElementException, 
    ElementClickInterceptedException
)
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


class BaseWebDriver:
    """WebDriver基础管理类"""
    
    # 随机User-Agent列表 (5个不同的真实浏览器标识)
    USER_AGENTS = [
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
    ]
    
    def __init__(self):
        """初始化基础WebDriver管理器"""
        self.driver = None
        self.wait = None
        self.selected_user_agent = random.choice(self.USER_AGENTS)
        print(f"选择的User-Agent: {self.selected_user_agent}")
    
    def setup_driver(self):
        """初始化Chrome WebDriver"""
        try:
            print("正在初始化Chrome WebDriver...")
            
            # Chrome选项配置
            options = Options()
            options.add_argument('--no-sandbox')
            options.add_argument('--disable-dev-shm-usage')
            options.add_argument('--disable-gpu')
            options.add_argument('--window-size=1920,1080')
            
            # 启用无头模式 (爬虫专业模式)
            options.add_argument('--headless')
            print("已启用无头模式")
            
            # 设置随机User-Agent
            options.add_argument(f'--user-agent={self.selected_user_agent}')
            
            # 禁用图片和CSS加载以提高速度
            prefs = {
                "profile.managed_default_content_settings.images": 2,
                "profile.managed_default_content_settings.stylesheets": 2,
                "profile.managed_default_content_settings.plugins": 2,
                "profile.managed_default_content_settings.popups": 2,
                "profile.managed_default_content_settings.geolocation": 2,
                "profile.managed_default_content_settings.notifications": 2,
                "profile.managed_default_content_settings.media_stream": 2,
            }
            options.add_experimental_option("prefs", prefs)
            
            # 其他反检测设置
            options.add_argument('--disable-blink-features=AutomationControlled')
            options.add_experimental_option("excludeSwitches", ["enable-automation"])
            options.add_experimental_option('useAutomationExtension', False)
            
            # 尝试多种ChromeDriver初始化方法
            try:
                # 方法1：使用webdriver-manager自动管理
                from webdriver_manager.chrome import ChromeDriverManager
                service = Service(ChromeDriverManager().install())
                self.driver = webdriver.Chrome(service=service, options=options)
            except Exception as e1:
                print(f"webdriver-manager失败: {e1}")
                try:
                    # 方法2：使用默认系统路径
                    self.driver = webdriver.Chrome(options=options)
                except Exception as e2:
                    print(f"默认路径失败: {e2}")
                    # 方法3：尝试常见的ChromeDriver路径
                    common_paths = [
                        '/usr/local/bin/chromedriver',
                        '/opt/homebrew/bin/chromedriver',
                        '/usr/bin/chromedriver'
                    ]
                    
                    driver_found = False
                    for path in common_paths:
                        if os.path.exists(path):
                            try:
                                service = Service(path)
                                self.driver = webdriver.Chrome(service=service, options=options)
                                driver_found = True
                                print(f"使用ChromeDriver路径: {path}")
                                break
                            except Exception as e3:
                                continue
                    
                    if not driver_found:
                        raise Exception(f"无法初始化ChromeDriver。请确保已安装Chrome浏览器和ChromeDriver。\n原始错误: {e1}")
            
            # 设置等待实例
            self.wait = WebDriverWait(self.driver, 20)
            
            # 设置反检测脚本
            self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
            
            print("WebDriver初始化成功")
            
        except Exception as e:
            print(f"WebDriver初始化失败: {e}")
            raise
    
    def wait_for_page_load(self):
        """
        等待页面加载完成 - 基于成功经验的智能等待机制
        使用渐进式检测，在20秒内完成页面加载检查
        """
        print("  ⏳ 智能等待页面加载...")
        
        try:
            # 第一阶段：基础DOM准备检查（基于成功经验）
            print("  🔄 DOM结构检查...")
            self.wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
            print("    ✓ DOM结构已就绪")
            
            # 第二阶段：渐进式内容检查（基于成功经验）
            print("  🔍 渐进式内容加载检查...")
            max_wait = 12  # 少于稳定版的 15秒
            check_interval = 3
            
            content_loaded = False
            for i in range(0, max_wait, check_interval):
                time.sleep(check_interval)
                
                try:
                    # 检查页面内容是否已加载
                    body_text = self.driver.find_element(By.TAG_NAME, "body").text
                    if len(body_text) > 1000:
                        print(f"    ✅ 页面内容已加载 (第 {i+check_interval} 秒)")
                        content_loaded = True
                        break
                except Exception:
                    continue
            
            if not content_loaded:
                print("    ⚠️  内容加载检查超时，继续执行")
            
            # 第三阶段：检查车型轮播组件
            print("  🎠 检查车型轮播组件...")
            carousel_elements = self.driver.find_elements(
                By.CSS_SELECTOR, 'div[data-cy][class*="ProductTile"]'
            )
            
            if carousel_elements:
                print(f"    ✅ 找到 {len(carousel_elements)} 个车型元素")
            else:
                print("    ⚠️  暂未检测到车型元素，可能需要更多等待时间")
                # 额外等待轮播组件加载
                time.sleep(3)
            
        except TimeoutException:
            print("    ⚠️  页面加载超时，但继续执行流程")
        except Exception as e:
            print(f"    ⚠️  等待页面加载时出现异常: {e}")
    
    def progressive_page_wait(self):
        """渐进式页面等待 - 适配福特网站特点"""
        print("  📡 启动渐进式页面检查...")
        
        # 等待基础DOM
        try:
            self.wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
            print("    ✓ 基础DOM已加载")
        except TimeoutException:
            print("    ❌ DOM加载超时")
            return False
        
        # 检查是否有轮播指示器（表明轮播组件已初始化）
        time.sleep(2)
        carousel_indicators = self.driver.find_elements(By.CSS_SELECTOR, 'span[class*="Pagination"]')
        if carousel_indicators:
            print("    ✓ 轮播组件已初始化")
        
        return True
    
    def click_element_safely(self, element):
        """安全地点击元素，处理各种异常情况"""
        try:
            # 滚动到元素
            self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", element)
            time.sleep(0.5)
            
            # 尝试普通点击
            try:
                element.click()
            except ElementClickInterceptedException:
                # 使用JavaScript点击
                self.driver.execute_script("arguments[0].click();", element)
                
        except Exception as e:
            # 最后尝试：强制JavaScript点击
            try:
                self.driver.execute_script("arguments[0].click();", element)
            except Exception:
                raise e
    
    def verify_page_interactivity(self):
        """验证页面是否可交互 - 基于稳定版测试经验"""
        try:
            # 检查页面是否响应基本交互
            body = self.driver.find_element(By.TAG_NAME, "body")
            
            # 检查页面是否仍在加载
            ready_state = self.driver.execute_script("return document.readyState")
            if ready_state != "complete":
                return False
            
            # 检查是否有阻止交互的覆盖层
            overlays = self.driver.find_elements(
                By.CSS_SELECTOR, 
                '[style*="position: fixed"], [style*="z-index"], .modal, .overlay'
            )
            
            blocking_overlays = 0
            for overlay in overlays:
                try:
                    if overlay.is_displayed():
                        style = overlay.get_attribute('style')
                        if 'position: fixed' in style or 'z-index' in style:
                            blocking_overlays += 1
                except:
                    continue
            
            return blocking_overlays == 0
            
        except Exception:
            return True  # 如果检测失败，假设页面可交互
    
    def force_cleanup_blocking_elements(self):
        """强制清理可能阻挡交互的页面元素"""
        try:
            print("        🧹 清理页面阻挡元素...")
            
            cleanup_script = """
            var overlays = document.querySelectorAll('[style*="position: fixed"], [style*="z-index"], .modal, .overlay');
            var removedCount = 0;
            
            overlays.forEach(function(el) {
                var style = window.getComputedStyle(el);
                var zIndex = parseInt(style.zIndex);
                
                if (zIndex > 1000 || style.position === 'fixed') {
                    el.style.display = 'none';
                    el.style.visibility = 'hidden';
                    el.style.pointerEvents = 'none';
                    removedCount++;
                }
            });
            
            // 恢复页面交互
            document.body.style.overflow = 'auto';
            document.body.style.pointerEvents = 'auto';
            
            return removedCount;
            """
            
            removed = self.driver.execute_script(cleanup_script)
            if removed > 0:
                print(f"        ✓ 清理了 {removed} 个可能的阻挡元素")
            
        except Exception as e:
            print(f"        清理阻挡元素时出错: {e}")
    
    def cleanup(self):
        """清理资源"""
        try:
            if self.driver:
                self.driver.quit()
                print("WebDriver已关闭")
        except Exception as e:
            print(f"清理WebDriver时出错: {e}")


class PageUtilities:
    """页面工具类，包含常用的页面操作方法"""
    
    @staticmethod
    def parse_url_parameters(url: str) -> tuple:
        """
        从福特官网URL中解析关键参数
        
        Args:
            url (str): 福特官网配置页面URL
            
        Returns:
            tuple: (catalog_id, series_code, default_paint)
        """
        catalog_id = ""
        series_code = ""
        default_paint = ""
        
        try:
            if '?' in url:
                query_string = url.split('?')[1]
                params = query_string.split('&')
                
                for param in params:
                    if '=' in param:
                        key, value = param.split('=', 1)
                        if key == 'catalogId':
                            catalog_id = value
                        elif key == 'series':
                            series_code = value
                        elif key == 'paint':
                            default_paint = value
                            
            print(f"解析URL参数: catalogId={catalog_id}, series={series_code}, paint={default_paint}")
            
        except Exception as e:
            print(f"解析URL参数失败: {e}")
            
        return catalog_id, series_code, default_paint